package com.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.model.po.DeliveryAddress;
import com.springboot.service.DeliveryAddressService;

import util.VerifyUtil;

@RefreshScope	//监听动态刷新
@RestController
@RequestMapping("/deliveryAddresses")
public class DeliveryAddressController {

	@Autowired
	DeliveryAddressService deliveryAddressService;
	
	@GetMapping("/users/{userId}")
	public ResponseEntity listDeliveryAddressByUserId(@PathVariable String userId , @RequestParam String token)throws Exception{
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !userId.equals(userId_target)) {	//不能查找他人的信息
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		List<DeliveryAddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
		if(list.size() == 0 || list == null) {
			return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("The requested resource was not found.");
		}else {
			return ResponseEntity.ok(list);
		}
	}
	
//	这个功能不开放
//	@GetMapping("/{daId}")
//	public ResponseEntity getDeliveryAddressById(@PathVariable Integer daId)throws Exception {
//		DeliveryAddress result = deliveryAddressService.getDeliveryAddressById(daId);
//		if(result != null) {
//			return ResponseEntity.ok(result);
//		}else {
//			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("The requested resource was not found.");
//		}
//	}

	@PostMapping
	public ResponseEntity saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress , @RequestParam String token) throws Exception{
		//{contactName},#{contactSex},#{contactTel},#{address},#{userId}
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !deliveryAddress.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		int result = deliveryAddressService.saveDeliveryAddress(deliveryAddress);
		if(result == 1) {
			return ResponseEntity.ok("ok");
		}else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred");
		}
	}

	@PutMapping
	//存储更新的，修改旧的为remark=1
	public ResponseEntity updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress , @RequestParam String token)throws Exception {
		//{contactName},#{contactSex},#{contactTel},#{address},#{userId},daId	
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !deliveryAddress.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		int result = deliveryAddressService.updateDeliveryAddress(deliveryAddress);
		if(result == 1) {
			return ResponseEntity.ok("ok");
		}else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred");
		}
	}

	@DeleteMapping("/{daId}")
	//删除的修改remark=1
	public ResponseEntity removeDeliveryAddress(@PathVariable Integer daId , @RequestParam String token)throws Exception {
		String userId_target = VerifyUtil.verify(token);
		DeliveryAddress da = deliveryAddressService.getDeliveryAddressById(daId);
		if(userId_target == null || !da.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		int result = deliveryAddressService.removeDeliveryAddress(daId);
		if(result == 1) {
			return ResponseEntity.ok("ok");
		}else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred");
		}
	}

}
